package com.abaaba.project2.service.serviceImpl;

import com.abaaba.project2.bean.Admin;
import com.abaaba.project2.bean.AdminExample;
import com.abaaba.project2.bean.bo.AdminListBo;
import com.abaaba.project2.bean.vo.AdminListVo;
import com.abaaba.project2.mapper.AdminMapper;
import com.abaaba.project2.service.AdminSystemService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AdminSystemServiceImpl implements AdminSystemService {

    @Autowired
    AdminMapper adminMapper;

    @Override
    public AdminListVo showAdminList(AdminListBo adminListBo) {

        //分页设置
        PageHelper.startPage(adminListBo.getPage(),adminListBo.getLimit());
        //Bo封装example
        AdminExample adminExample = new AdminExample();
        adminExample.setOrderByClause(adminListBo.getSort() + " " +adminListBo.getOrder());
        AdminExample.Criteria criteria = adminExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        if(adminListBo.getUsername() != null && !"".equals(adminListBo.getUsername()))
        {
            criteria.andUsernameLike("%"+adminListBo.getUsername()+"%");
        }

        //调用Dao层
        List<Admin> admins = adminMapper.selectByExample(adminExample);


        return new AdminListVo((int) new PageInfo<Admin>(admins).getTotal(),admins);
    }

    @Override
    public Admin createAdmin(Admin admin) {

        //先查一下，如果重名，返回Null
        AdminExample adminExample = new AdminExample();
        AdminExample.Criteria criteria = adminExample.createCriteria();
        criteria.andUsernameEqualTo(admin.getUsername());

        List<Admin> admins1 = adminMapper.selectByExample(adminExample);

        if(admins1.size() == 1)
        {
            return  null;
        }


        Date date = new Date(System.currentTimeMillis());
        admin.setAddTime(date);
        admin.setUpdateTime(date);




        adminMapper.insertSelective(admin);

        List<Admin> admins = adminMapper.selectByExample(adminExample);

        Admin adminVo = admins.get(0);

        return adminVo;
    }

    @Override
    public Admin updateAdmin(Admin admin) {

        //先查一下，如果重名，返回Null
        AdminExample adminExample = new AdminExample();
        AdminExample.Criteria criteria = adminExample.createCriteria();
        criteria.andUsernameEqualTo(admin.getUsername()).andIdNotEqualTo(admin.getId());

        List<Admin> admins1 = adminMapper.selectByExample(adminExample);

        if(admins1.size() == 1)
        {
            return  null;
        }

        Date date = new Date(System.currentTimeMillis());
        admin.setUpdateTime(date);

        AdminExample adminExample1 = new AdminExample();
        AdminExample.Criteria criteria1 = adminExample1.createCriteria();
        criteria1.andIdEqualTo(admin.getId());

        adminMapper.updateByExampleSelective(admin,adminExample1);
        List<Admin> admins = adminMapper.selectByExample(adminExample1);

        Admin adminVo = admins.get(0);

        return adminVo;
    }

    @Override
    public void deleteAdmin(Admin admin) {

        Admin deleteMark = new Admin();
        deleteMark.setDeleted(true);
        deleteMark.setId(admin.getId());

        AdminExample adminExample = new AdminExample();
        AdminExample.Criteria criteria = adminExample.createCriteria();
        criteria.andIdEqualTo(deleteMark.getId());

        adminMapper.updateByExampleSelective(deleteMark,adminExample);

    }
}
